package com.tutu.mybatis.dynamic.datasource.util;

import java.util.Random;

/**
 * @author qyy
 * @date 2021/3/18 10:24
 */
public class UserInfoHolder {

    public static ThreadLocal<Long> USER = new ThreadLocal<>();

    /**
     * 模拟用户id
     *
     * @return userId
     */
    public static Long getUserId() {
        Long userId = USER.get();
        if (userId == null) {
            long[] idArray = {1};
            Random random = new Random();
            int index = random.nextInt(100) % idArray.length;
            userId = idArray[index];
            USER.set(userId);
        }
        return userId;
    }

    public static void remove() {
        USER.remove();
    }
}
